//
//  LLMainViewController.m
//  lrlzapp
//
//  Created by dl on 16/2/23.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLMainViewController.h"
#import "LLMainViewModel.h"
#import "LLBannerCollectionViewCell.h"
#import "LLBottomTitleCollectionViewCell.h"
#import "LLImgCollectionViewCell.h"
#import "PortalMainActivityTiledHeaderView.h"
#import "LLRefresh.h"

@interface LLMainViewController () <UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout>
@property(nonatomic, strong, readonly) LLMainViewModel *viewModel;
@property(weak, nonatomic) IBOutlet UICollectionView *collectionView;
@end

@implementation LLMainViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[LLBannerCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_BANNER];
    [self.collectionView registerClass:[LLBottomTitleCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_BOTTOM_TITLE];
    [self.collectionView registerClass:[LLImgCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_IMG];
    [self.collectionView registerClass:[PortalMainActivityTiledHeaderView class]
            forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
                   withReuseIdentifier:IDENTIFIER_HEADER_ACTIVITY_TILED];

    @weakify(self)
    self.collectionView.header = [LLRefreshNomalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self.viewModel.loadMenusCommand execute:nil];
    }];
}

- (void)bindViewModel {
    [super bindViewModel];

    @weakify(self)
    [[RACObserve(self.viewModel, dataSource)
            subscribeOn:[RACScheduler mainThreadScheduler]]
            subscribeNext:^(id x) {
                @strongify(self)
                [self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 4)]];
//         [self.collectionView reloadData];
            }];

    [[RACObserve(self.viewModel, isLoading)
            distinctUntilChanged]
            subscribeNext:^(NSNumber *isLoading) {
                @strongify(self)
                if ([isLoading boolValue]) {
                    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES].labelText = @"Logging in...";
                } else {
                    [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
                    [self.collectionView.header endRefreshing];
                }
            }];

    [self.viewModel.loadMenusCommand execute:nil];
}

#pragma mark -
#pragma mark UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([self.viewModel.dataSource[section] count] == 0) {
        return 0;
    }
    if (section == 0 && self.viewModel.bannerDataSource.count > 0) {
        return 1;
    } else if (section == 1) {
        return self.viewModel.quickEntryDataSource.count;
    } else if (section == 2) {
        return self.viewModel.hotActivityDataSource.count;
    } else if (section == 3) {
        return self.viewModel.activityTiledDataSource.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LLBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_BANNER
                                                                                     forIndexPath:indexPath];
        NSObject *viewModel = self.viewModel.bannerDataSource[indexPath.item];
        [cell bindViewModel:viewModel];
        return cell;
    } else if (indexPath.section == 1) {
        LLBottomTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_BOTTOM_TITLE
                                                                                          forIndexPath:indexPath];
        NSObject *viewModel = self.viewModel.quickEntryDataSource[indexPath.item];
        [cell bindViewModel:viewModel];
        return cell;
    } else if (indexPath.section == 2) {
        LLImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_IMG
                                                                                  forIndexPath:indexPath];
        NSObject *viewModel = self.viewModel.hotActivityDataSource[indexPath.item];
        [cell bindViewModel:viewModel];
        return cell;
    } else {
        LLImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_COLLECTIONVIEW_CELL_IMG
                                                                                  forIndexPath:indexPath];
        NSObject *viewModel = self.viewModel.activityTiledDataSource[indexPath.item];
        [cell bindViewModel:viewModel];
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    PortalMainActivityTiledHeaderView *headerView = nil;
    if (kind == CHTCollectionElementKindSectionHeader && indexPath.section == 3) {
        headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:IDENTIFIER_HEADER_ACTIVITY_TILED forIndexPath:indexPath];
        headerView.moduleTitle = self.viewModel.tiledActivityTitle;
    }
    return headerView;
}

#pragma mark -
#pragma mark UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(FULL_WIDTH, SUIT_H_ALL(150));
    } else if (indexPath.section == 1) {
        return CGSizeMake(FULL_WIDTH / 4.f, SUIT_H(86));
    } else if (indexPath.section == 2) {
        return indexPath.item == 0 ? CGSizeMake(185, 170) : CGSizeMake(185, 83);
    } else if (indexPath.section == 3) {
        return CGSizeMake(FULL_WIDTH, SUIT_H_ALL(150));
    }
    return CGSizeZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section {
    if (section == 3 && self.viewModel.activityTiledDataSource.count > 0) {
        return SUIT_H(36);
    }
    return 0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout columnCountForSection:(NSInteger)section {
    if (section == 1) {
        return 4;
    } else if (section == 2) {
        return 2;
    }
    return 1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 2 || section == 3) {
        return SUIT_H(4);
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumColumnSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 2) {
        return SUIT_W(5);
    }
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 2) {
        return UIEdgeInsetsMake(8, 0, 8, 0);
    }
    return UIEdgeInsetsZero;
}

@end
